<?php
/**
 * Facebook 特有项目的 Ajax 处理
 */

include_once '_inc.php';

function def(ujn_controller $c) {
	echo '404';
}

ujn::runApplication(
	require_once ROOT . '/configs/config.php',
	( isset($_REQUEST['cmd']) && function_exists($_REQUEST['cmd']) ) ? $_REQUEST['cmd'] : 'def'
);

function show_user_view() {
	echo 'aaaaaassss';
	exit;
	$puid = $_REQUEST['puid'];
}

//////////////////////////////////////////////////////

function save_friends() {
	$mid = tls_func::uint($_REQUEST['mid']);
	$friends = $_REQUEST['friends'];

	$flag = mdl_other_friends::getInstance()->saveRow( $mid, $friends );
	
	die(json_encode(array('flag' => $flag)));
}

function init_battle() {
	$data = array();
	$mid = $_REQUEST['mid'];
	$sex = $_REQUEST['sex'];
	
	if(mdl_member_vote::getInstance()->check($mid)) {
		$data['flag'] = -10;	//已用完投票數
		die(json_encode($data));
	}
	
	$aBattle = mdl_member_vote::getInstance()->rand($sex);
	
	$data['flag'] = 1;
	$data['new_battle'] = $aBattle;
	die(json_encode($data));
}

function do_battle() {
	$data = array();
	$data['flag'] = 0;
	
	$mid = $_REQUEST['mid'];
	$pic_l_sitemid = $_REQUEST['pic_l'];
	$pic_r_sitemid = $_REQUEST['pic_r'];
	$w = $_REQUEST['win'];
	
	if(mdl_member_vote::getInstance()->check($mid)) {
		$data['flag'] = -10;	//已用完投票數
		die(json_encode($data));
	}
	
	if(mdl_member_vote::getInstance()->vote($mid, $pic_l_sitemid, $pic_r_sitemid, $w)) {
		$data['flag'] = 1;
		$data['new_battle'] = mdl_member_vote::getInstance()->rand($sex);
		$data['results'] = mdl_member_vote::getInstance()->search(array($pic_l_sitemid, $pic_r_sitemid));
		$data['surplus'] = mdl_member_vote::getInstance()->getSurplus($mid);
	}
	
	die(json_encode($data));
}